<%@Page Language="VB"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Repeated-Value Data Binding to a HashTable</title>
<style type="text/css">
body, td {font-family:Tahoma,Arial,sans-serif; font-size:10pt}
input {font-family:Tahoma,Arial,sans-serif; font-size:9pt}
.heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold}
.subhead {font-family:Tahoma,Arial,sans-serif; font-size:12pt; font-weight:bold; padding-bottom:5px}
.cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}
</style></head>
<body bgcolor="#ffffff">
<span class="heading">Repeated-Value Data Binding to a HashTable</span><hr />
<!--------------------------------------------------------------------------->

<table border="0">
<tr><td nowrap="nowrap">


HTML <b>&lt;select&gt;</b> elements:<br />
<select id="MySelectList1" runat="server" /> &nbsp;
<select id="MySelectList2" runat="server" /><p />

<b>&lt;ASP:DropDownList&gt;</b> controls:<br />
<ASP:DropDownList id="MyDropDown1" runat="server" /> &nbsp;
<ASP:DropDownList id="MyDropDown2" runat="server" /><p />

<b>&lt;ASP:ListBox&gt;</b> controls:<br />
<ASP:ListBox id="MyASPList1" runat="server" /> &nbsp;
<ASP:ListBox id="MyASPList2" runat="server" /><p />

<b>&lt;ASP:DataGrid&gt;</b> control:<br />
<ASP:DataGrid id="MyDataGrid" runat="server" AutoGenerateColumns="false">
  <Columns>
    <ASP:BoundColumn HeaderText="Key" DataField="Key" />
    <ASP:BoundColumn HeaderText="Value" DataField="Value" DataFormatString="{0:c}" />
  </Columns>
</ASP:DataGrid><p />

<b>&lt;ASP:Repeater&gt;</b> control:<br />
<ASP:Repeater id="MyRepeater" runat="server">
  <ItemTemplate>
    <%# Container.DataItem.Key %> =
    <%# Container.DataItem.Value %><br />
  </ItemTemplate>
</ASP:Repeater><p />


</td><td nowrap="nowrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</td><td nowrap="nowrap">


<b>&lt;ASP:DataList&gt;</b> control:<br />
<ASP:DataList id="MyDataList" runat="server">
  <ItemTemplate>
    '<%# Container.DataItem.Key %>' - value:
    <%# DataBinder.Eval(Container.DataItem, "Value", "{0:E}") %>
  </ItemTemplate>
</ASP:DataList><p />

<b>&lt;ASP:CheckBoxList&gt;</b> control:<br />
<ASP:CheckBoxList id="MyCheckList" runat="server" /><p />

<b>&lt;ASP:RadioButtonList&gt;</b> control:<br />
<ASP:RadioButtonList id="MyRadioList" runat="server" /><p />


</td></tr>
</table>

<!--------------------------------------------------------------------------->

<script language="vb" runat="server">

Sub Page_Load()

   'create a HashTable of values to bind to
   Dim tabValues As New HashTable(5)
   tabValues.Add("Microsoft", 49.56)
   tabValues.Add("Sun", 28.33)
   tabValues.Add("IBM", 55)
   tabValues.Add("Compaq", 20.74)
   tabValues.Add("Oracle", 41.1)

   'set the DataSource property of the controls

   'first <select> displays the Keys in the HashTable
   MySelectList1.DataSource = tabValues
   MySelectList1.DataTextField = "Key"

   'second one displays the Values in the HashTable
   'and uses the Keys as the <option> values
   MySelectList2.DataSource = tabValues
   MySelectList2.DataValueField = "Key"
   MySelectList2.DataTextField = "Value"

   'same applies to ASP: controls, except here
   'we can also specify the format of the Key
   MyDropDown1.DataSource = tabValues
   MyDropDown1.DataTextField = "Key"
   MyDropDown2.DataSource = tabValues
   MyDropDown2.DataValueField = "Key"
   MyDropDown2.DataTextField = "Value"
   MyDropDown2.DataTextFormatString = "{0:F}"

   MyASPList1.DataSource = tabValues
   MyASPList1.DataTextField = "Key"
   MyASPList2.DataSource = tabValues
   MyASPList2.DataValueField = "Key"
   MyASPList2.DataTextField = "Value"
   MyASPList2.DataTextFormatString = "{0:C}"

   'to bind a HashTable to a DataGrid, Repeater or DataList
   'we have to specify the columns. This is done within the
   'element definitions above using an <ItemTemplate>, so
   'we just have to specify the DataSource in the code here
   MyDataGrid.DataSource = tabValues
   MyRepeater.DataSource = tabValues
   MyDataList.DataSource = tabValues

   'in the CheckboxList we'll display the Title and
   'use the Value as the control value
   MyCheckList.DataSource = tabValues
   MyCheckList.DataValueField = "Value"
   MyCheckList.DataTextField = "Key"

   'in the RadioList we'll display and format the
   'Value and use the Key as the control value
   MyRadioList.DataSource = tabValues
   MyRadioList.DataValueField = "Key"
   MyRadioList.DataTextField = "Value"
   MyRadioList.DataTextFormatString = "Percentage rate {0:F}%"

   'finally, bind all the controls on the page
   Page.DataBind()

End Sub

</script>

<!--------------------------------------------------------------------------->
<hr /><span class="cite">[<a href="../global/viewsource.aspx">view source</a>] &nbsp; &nbsp; &nbsp; &nbsp;
&copy;2001 <a class="cite" href="http://www.wrox.com/">Wrox Press</a> -
<a class="cite" href="http://www.wrox.com/Books/Book_Details.asp?isbn=1861004885">Professional ASP.NET</a> (ISBN: 1-861004-88-5)</span>
</body>
</html>
